<?php
/*--------------------------------------------------------------------------/*
* MasterArray class. 
* Method of use : Static 
* 
* It is a group array mgmt functions to apply usual routines such as
* droping nulls, key sort, reindexing after poping, swap, and alot more
*
* Originally was part of PHP_WS cms system edited by Shoeb Surya
*                                                    shoeb.surya@gmail.com
*
* Last Edit: 05/16/2006: 10:14
* $id:Array.php
*
/*--------------------------------------------------------------------------*/

class MasterArray {
  /**
   * Uses the array given and swaps the value at index 1 with the value at index 2.
   *
   * @param  array $array  The array to use for the swap
   * @param  mixed $index1 The index of the first value to be swapped.
   * @param  mixed $index2 The index of the second value to be swapped.
   * @access public
   */
  function swap(&$array, $index1, $index2) {
    $temp = $array[$index1];
    $array[$index1] = $array[$index2];
    $array[$index2] = $temp;

  }//end func swap

  /**
   * Reindexes the supplied array from 0 to number of values - 1.
   *
   * @param  array $array The array to reindex.
   * @access public
   */
  function reindex(&$array) {
    $temp = $array;
    $array = array();
    foreach($temp as $value) {
      $array[] = $value;
    }
    
  }//end func reindex

  /**
   * Returns the key to the last element in $checkArray
   *
   * @param  array $check_array Array to use in check
   * @return mixed $maxKey      The maximum key in the supplied array
   * @access public
   */
  function maxKey($checkArray){
    if(is_array($checkArray)){
      end($checkArray);
      list($maxKey) = each($checkArray);
      return $maxKey;
    } else {
		  echo("MasterArray::maxKey: I did not recieve an array!");
      return FALSE;
    }
  }//end func maxKey

  /**
   * Repositions an array element one slot up or down its current position.
   *
   * @param  array  $array       Associative array to shove new cell into
   * @param  string $index       Index of the element to be moved
   * @param  mixed  $element     Value of the element to be moved (not required - use if you don't know what $index is ($index=NULL))
   * @param  bool   $moveup      Direction to move. Defaults to moving up (true).
   * @param  bool   $assoc_array Type of array.  Numeric arrays get re-indexed.
   * @return array  $new array   Finished array
   * @access public
   */
  function moveElement ($array, $index=NULL, $element=NULL, $moveup=true, $assoc_array=true) {
    /* parameter housekeeping */
    if (!is_array($array))
      echo("MasterArray::moveElement received a/an ".gettype($array)." variable, not an array<br />");
    /* If only the value was given, search for the key & delete it */
    if ($index==NULL) {
      $index = array_search($element, $array);
      if (!$index)
	echo("MasterArray::moveElement received ".gettype($array) . ", a value that doesn't exist in the array.<br />");
    }
    $element = $array[$index];
    
    /* Process the array */
    if ($moveup) 
      $array = array_reverse($array, TRUE);
    foreach ($array as $key=>$value) {
      if ($key == $index)
	$buffer[$key] = $element;
      else {
	$new_array[$key] = $value;
	if (isset($buffer)) {
	  $new_array[$index] = $element; 
	  unset($buffer);
	}
      }
    }
    if ($moveup) 
      $new_array = array_reverse($new_array, TRUE);

    /* Reindex the result */
    if (!$assoc_array && count($array)) {
      foreach ($new_array as $new_val)
	$numerical_array[] = $new_val;
      $new_array = $numerical_array;
    }
    return $new_array;

  }//end func moveElement

  /**
   * Removes an element from an array then reindexes the array
   *
   * @param array
   * @param mixed
   * @param mixed
   * @return array
   * @access public
   */
  function removeElement($array, $key=NULL, $value=NULL){
    if (is_array($array)){
      if (isset($key) && isset($value)){
        if ($array[$key] == $value)
          $new_key = $key;
        else 
          return NULL;
      } elseif (isset($key)){
        if (is_string($key) && !is_numeric($key)){
          unset($array[$key]);
          return $array;
        }
        else
          $new_key = $key;
      } elseif (isset($value))
	  $new_key = array_search($value, $array);
      else
        return NULL;

      unset($array[$new_key]);

      if (count($array)){
        foreach ($array as $new_val)
          $ret_array[] = $new_val;

        return $ret_array;
      } else
        return $array;

    } else 
      echo("MasterArray::removeElement: Variable sent to removeelement() was not an array");

  }//end funk removeElement


  /**
   * Inserts variables into an array based on an object
   *
   * @param  object $object The object to copy variables from
   * @param  array  $array  The array to copy variables to
   * @access public
   */
  function objectToArray($object, &$array){
    if (is_array($array) && is_object($object)){
      $object_info = (get_object_vars($object));
      foreach ($object_info as $key=>$value)
        $array[$key] = $value;
    } else
      echo("MasterArray::objectToArray: Missing array and/or object");

  }//end func objectToArray


  /**
   * Unsets array rows where the value is NULL
   *
   * @author Matthew McNaney <matt@NOSPAM.tux.appstate.edu>
   * @param  array $array Array to drop nulls from
   * @access public
   */
  function dropNulls(&$array){
    if (is_array($array)){
      reset ($array);
      foreach ($array as $key=>$value){
        if (is_null($value)){
          unset ($array[$key]);
        }
      }
    } else
      echo("MasterArray::dropNulls: Array not received!");

  }//end func dropNulls

  /**
   * Copy of php's ksort with a descending mode
   *
   * @param  array  &$array The array to sort
   * @param  string $mode   The direction to sort
   * @access public
   */
  function ksort(&$array, $mode="SORT_ASC"){
    if ($mode == "SORT_DESC")
      uksort($array, "cmp");
    else
      ksort($array);

  }//end func ksort

  /**
   * Creates an array of numbers from start to final in
   * increments of the interval variable
   *
   * Note that the function will start on the start variable. In other words
   * If you start on 1 and interval by 4, your array will say 1, 5, 9, etc.
   * If the final number surpasses 'final' it will not be included.
   *
   * @param  integer $final    Final number of the count
   * @param  integer $start    Number to begin counting on (not from)
   * @param  integer $interval Number to increment the count
   * @return array   Array of intervaled numbers
   * @access public
   */
  function interval($final, $start=0, $interval=1){
    $count = 0;
    if ($interval && $final > $start){
      for ($i=$start; $i <= $final; $i = $i+$interval){
	$count++;
	$ret_array[] = $i;
	if ($count > 10000)
	  echo("MasterArray::interval: Interval is too large");
      }
      return $ret_array;
    } else
      return NULL;

  }//end func interval

}//end class Array
?>

